package com.baselibrary.view.share;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.baselibrary.R;
import com.baselibrary.bean.ShareItemBean;
import com.baselibrary.databinding.ViewShareLayoutBinding;
import com.baselibrary.utils.BasePopupWindow;
import com.baselibrary.utils.CommonUtil;

import java.util.List;

public class SharePopupWindow extends BasePopupWindow {


    public static SharePopupWindow mSingleton = null;
    private ViewShareLayoutBinding mBinding;


    public static SharePopupWindow getInstance(Context context) {
        if (mSingleton == null) {
            synchronized (SharePopupWindow.class) {
                if (mSingleton == null) {
                    mSingleton = new SharePopupWindow(context);
                }
            }
        }
        return mSingleton;
    }

    public SharePopupWindow(Context context) {
        super(context);
    }

    @Override
    protected int animationStyle() {
        return R.style.PopupAnimation;
    }

    @Override
    protected View initContentView() {
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_share_layout, null, false);
        mBinding.rvShareApp.setAdapter(mShareAppAdapter);
        mBinding.rvShareFriends.setAdapter(mShareFriendAdapter);
        return mBinding.getRoot();
    }

    public SharePopupWindow setShareItem(boolean isShowPoster, boolean isShowDown, boolean isShowReport) {
        mList.add(new ShareItemBean("私信", R.drawable.ic_private_friends, ShareItemBean.PRIVATE_LETTER, 0));
        mList.add(new ShareItemBean("微信", R.drawable.icon_share_wechat, ShareItemBean.WX, 0));
        mList.add(new ShareItemBean("朋友圈", R.drawable.icon_share_friends, ShareItemBean.WXZone, 0));
        if (isShowPoster)
            mList.add(new ShareItemBean("生成海报", R.drawable.icon_save_picture, ShareItemBean.POSTER, 0));
        if (isShowDown)
            mList.add(new ShareItemBean("保存至相册", R.drawable.icon_save_picture, ShareItemBean.DOWN, 0));
        if (isShowReport)
            mList.add(new ShareItemBean("举报", R.drawable.ic_report, ShareItemBean.REPORT, 0));
        mShareAppAdapter.setIsUpLayoutParams(true);
        mShareAppAdapter.setNewData(mList);
        return this;
    }

    public SharePopupWindow setShareItem(boolean privateLetter, boolean isShowPoster, boolean isShowDown, boolean isShowReport) {
        if (privateLetter) {
            mList.add(new ShareItemBean("私信", R.drawable.ic_private_friends, ShareItemBean.PRIVATE_LETTER, 0));
        }
        mList.add(new ShareItemBean("微信", R.drawable.icon_share_wechat, ShareItemBean.WX, 0));
        mList.add(new ShareItemBean("朋友圈", R.drawable.icon_share_friends, ShareItemBean.WXZone, 0));
        if (isShowPoster)
            mList.add(new ShareItemBean("生成海报", R.drawable.icon_save_picture, ShareItemBean.POSTER, 0));
        if (isShowDown)
            mList.add(new ShareItemBean("保存至相册", R.drawable.icon_save_picture, ShareItemBean.DOWN, 0));
        if (isShowReport)
            mList.add(new ShareItemBean("举报", R.drawable.ic_report, ShareItemBean.REPORT, 0));
        mShareAppAdapter.setIsUpLayoutParams(true);
        mShareAppAdapter.setNewData(mList);
        return this;
    }

    public SharePopupWindow setShareItem(boolean isUpLayout) {
        mList.add(new ShareItemBean("微信", R.drawable.icon_share_wechat, ShareItemBean.WX, 0));
        mList.add(new ShareItemBean("朋友圈", R.drawable.icon_share_friends, ShareItemBean.WXZone, 0));
        mShareAppAdapter.setIsUpLayoutParams(true);
        mShareAppAdapter.setNewData(mList);
        return this;
    }

    public SharePopupWindow setShareFriendAdapter(List<ShareItemBean> mList) {
        mBinding.llShareFriends.setVisibility(mList.size()==0?View.GONE:View.VISIBLE);
        mShareFriendAdapter.setNewData(mList);
        return this;
    }


    public void setListener(OnShareClickListener onClickListener) {
        mBinding.rlRoot.setOnClickListener(v -> {
            dismiss();
        });
        mBinding.rlCancel.setOnClickListener(view -> dismiss());
        mShareAppAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ShareItemBean bean = (ShareItemBean) adapter.getData().get(position);
            if (bean.getType() == ShareItemBean.PRIVATE_LETTER) {
                //私信
                if (onClickListener != null) onClickListener.onShareFriendClick(bean);
                dismiss();
            } else {
                //正常三方分享
                if (onClickListener != null) onClickListener.onShareClick(bean.getType());
            }
        });
        mShareFriendAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ShareItemBean bean = (ShareItemBean) adapter.getData().get(position);
            if (onClickListener != null) onClickListener.onShareFriendClick(bean);
            dismiss();
        });
    }

    public SharePopupWindow addItem(ShareItemBean bean) {
        mList.add(bean);
        return this;
    }


    @Override
    protected int initWidth() {
        return CommonUtil.getScreenWidth(context);
    }

    @Override
    protected int initHeight() {
        return -1;
    }


    public interface OnShareClickListener {
        void onShareClick(int type);

        void onShareFriendClick(ShareItemBean bean);
    }
}